<?php
class PhotoModel extends Model {

	function __construct()
		{
			parent::Model();
			$this->load->library('ImageLib');
			$this->load->model('CategoryModel');
		}

	public function action($type, $id)
		{
			switch ($type) {
				case "add":
					$this->addPhoto($id);
					break;
				case "edit":
				   $this->editPhoto($id);
					break;
				case "delete":
					$this->deletePhoto($id);
					break;
				}
		}

	private function deletePhoto($id)
		{
			$photo = $this->getPhoto($id);
			
			if (isset($photo->thumb) && file_exists(substr($photo->thumb, 1))) {
				unlink(substr($photo->thumb, 1));
			}
			if (isset($photo->image) && file_exists(substr($photo->image, 1))) {
				unlink(substr($photo->image, 1));
			}
			$photo->delete();
			echo "Фотография ".$this->input->post('name',true)." удалена";
			return $photo->category_id;
		}

	private function addPhoto($id)
		{
			$photo = new Photos();
			$photo->description = $this->input->post('description',true);
			$photo->category_id = $id;
			$photo->created_at = date();
			$cat = $this->CategoryModel->getCategory($id);
			
			if ($cat !== false) {
				$slug = strtolower($cat->slug);
				$image = $this->imagelib->upload('photo/'.$slug, true, true, 150, 400, '');
				$photo->image = '/res/img/photo/'.$slug.'/'.$image['file_name'];
				$photo->thumb = '/res/img/photo/'.$slug.'/thumb/'.$image['file_name'];
			}
			
			if ($this->input->post('name',true)) {
				$photo->name = $this->input->post('name',true);
			}else{
				$photo->name = $image['raw_name'];
			}
			$photo->save();
			redirect('/admin/category/view/'.$photo->category_id.'/');
		}

	private function editPhoto($id)
		{
			$photo = $this->getPhoto($id);
			//print_r($photo);
			$photo->name = $this->input->post('name',true);
			$photo->description = $this->input->post('description',true);
			$photo->category_id = $this->input->post('category',true);


			if ($_FILES['userfile']['name'] != '') 
			{
				$cat = $this->CategoryModel->getCategory($this->input->post('category',true));

				$slug = strtolower($cat->slug);
					
				if (isset($photo->thumb) && file_exists(substr($photo->thumb, 1))) {
					unlink(substr($photo->thumb, 1));
				}
				if (isset($photo->image) && file_exists(substr($photo->image, 1))) {
					unlink(substr($photo->image, 1));
				}
				
				$image = $this->imagelib->upload('photo/'.$slug, true, true, 150, 400, '');
				$photo->image = '/res/img/photo/'.$slug.'/'.$image['file_name'];
				$photo->thumb = '/res/img/photo/'.$slug.'/thumb/'.$image['file_name'];
			}

			$photo->save();
			redirect('/admin/photo/view/'.$photo->id.'/');
		}

	function getPhoto($id, $array = false)
		{
			$photo = Doctrine::getTable('Photos')->find($id);
			
			if($photo !== false) {
				if ($array) {
					return $photo->toArray(true);
				}else{
					return $photo;
				}
			}else{
				show_404('page');
			}
		}

	function getPhotoByCategory($id, $array = false)
		{
			$photo = Doctrine::getTable('Photos')->PhotoByCat($id);
			
			if($photo !== false) {
				if ($array) {
					return $photo->toArray(true);
				}else{
					return $photo;
				}
			}else{
				show_404('page');
			}
		}
		
	
}
?>